const Missile = require('MissileCurve')

cc.Class({
    extends: cc.Component,

    properties: {
        tank: cc.Prefab,
        preMissile: cc.Prefab,
        ground: cc.Node,
        angel: cc.Label,
    },

    // use this for initialization
    onLoad() {
        // 开启碰撞检测
        cc.director.getCollisionManager().enabled = true
        this.node.on('stanby', this.spawnMissile, this)
        this.node.on('touchstart', this.touchBegan, this)
        this.node.on('touchmove', this.touchMove, this)
        this.node.on('touchend', this.touchEnd, this)
        this.spawnMissile() // 生成导弹

        this.canva = cc.director.getWinSize();  // 获取设备可视区域尺寸     
        this.isUp = true
        this.scale = 7  // 按场景大小设置移动比率
        this.time = 0.05    // 设置时间速率
        this.speed = 60
    },

    touchBegan(e) {
        var degree = this.missileDir(this.node.convertToNodeSpaceAR(e.getLocation()), this.missile)
        this.angel.string = '角度：' + Math.round(degree * 10) / 10
        return true
    },
    touchMove(e) {
        var degree = this.missileDir(this.node.convertToNodeSpaceAR(e.getLocation()), this.missile)
        this.angel.string = '角度：' + Math.round(degree * 10) / 10
    },
    touchEnd(e) {
        if (this.moveAble) {
            let deg = this.missileDir(this.node.convertToNodeSpaceAR(e.getLocation()), this.missile)
            let theta = deg * Math.PI / 180
            this.velocity = new cc.Vec2()
            this.velocity.x = Math.cos(theta) * this.speed
            this.velocity.y = Math.sin(theta) * this.speed
            this.missile.emit('fire')
            this.moveAble = false
        }
    },
    update() {
        if (this.velocity) {
            if (this.isUp) {
                this.missile.y += 4
                if (this.missile.y >= 0)
                    this.isUp = false
            }
            else {
                this.curve()
            }
        }
    },
    // 导弹运动曲线
    curve() {
        let area = this.velocity.x * 0.5
        // 记录导弹下一位置
        let newPos = new cc.Vec2()
        // 计算导弹X轴下一位置
        newPos.x = this.missile.x + this.time * this.velocity.x * this.scale
        // 重新计算导弹Y轴分量
        let y_new = this.velocity.y - this.time * 9.8
        // 计算导弹Y轴下一位置
        newPos.y = this.missile.y + this.time * (y_new + this.velocity.y) * 0.5 * this.scale
        // 更新导弹Y轴分量
        this.velocity.y = y_new
        // 更新导弹指向
        this.missile.rotation = 90 - this.missileDir(newPos, this.missile)
        // 更新导弹位置
        var newGroundPos = this.ground.x - this.time * this.velocity.x * this.scale
        var szScreenWidth = this.canva.width * 0.5
        if (this.missile.x === 0) {
            if (newGroundPos < -szScreenWidth && newGroundPos > -this.ground.width + szScreenWidth) {
                this.ground.x = newGroundPos;
            }
            else {
                this.missile.x = newPos.x;
            }
        }
        else {
            this.missile.x = newPos.x;
            if (newPos.x < area && newPos.x > -area)
                this.missile.x = 0
        }
        this.missile.y = newPos.y

        // 导弹触地停止移动
        if (this.missile.y < -110) {
            this.velocity = null
            this.isUp = true
            this.missile.getComponent(Missile).explode()
            // this.missile.emit('explode')
        }
    },
    // 设置导弹发射角度
    missileDir(dest, sprite) {
        // 计算导弹目标位置与当前导弹位置的向量差
        let dir = cc.pSub(dest, sprite.position)

        // 计算导弹与目标向量弧度
        let theta = cc.radiansToDegrees(cc.pToAngle(dir))

        // 弧度转换为角度
        // sprite.rotation = 90 - cc.radiansToDegrees(theta)

        return theta
    },
    // 生成导弹
    spawnMissile() {
        this.moveAble = true
        this.velocity = null
        if (cc.pool.hasObject(Missile)) {
            this.missile = cc.pool.getFromPool(Missile).node
        }
        else {
            this.missile = cc.instantiate(this.preMissile)
        }
        this.node.addChild(this.missile)
        this.missile.setPosition(-420, -112)
        this.ground.x = -489
        this.missile.getComponent(Missile).init(this.node)
    },
});
